<template>
    <H2>用户理财赎回</H2>
    <table class="table table-bordered">
        <tr>
            <td>理财产品名称</td>
            <td>{{data.F_name }}</td>
        </tr>
        
        <tr>
            <td>年收益</td>
            <td>{{data.F_APY }}%</td>
        </tr>
        
        <tr>
            <td>最低理财金额</td>
            <td>{{ data.F_MinMoney.toFixed(2)}}CNY</td>
        </tr>
        
        <tr>
            <td>风险等级</td>
            <td>{{data.F_Level==0?"低风险":data.F_Level==1?"中风险":"高风险" }}</td>
        </tr>
        
        <tr>
            <td>用户购买金额</td>
            <td>{{data.UF_Money.toFixed(2) }}CNY</td>
        </tr>
        
        <tr>
            <td>赎回金额</td>
            <td>
                <input type="text" v-model="UFRmoney"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            <input type="button" @click="saveFinanRedeem" class="btn btn-primary" value="确认赎回" />
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router'
import { ref,onMounted } from 'vue'
import axios from 'axios';
// import { useUserStore } from '../../store/index.ts';

// const userStore = useUserStore()


const route = useRoute()
const router = useRouter()

const data=ref({
    F_APY:0,
    F_Level:0,
    F_MinMoney:0,
    F_id:0,
    F_name:'',
    UF_Id:0,
    UF_Money:0,
})

const UFRmoney=ref('')

console.log(route.query.UF_Id)

onMounted(()=>{
    loadData()
})

//加载数据
const loadData = ()=>{
    axios.get("https://localhost:44387/api/Finans/ShowUserFinanRedeem",{
        params:{
            UFid:route.query.UF_Id
        }
    }).then(res=>{
        console.log(res.data)
        data.value=res.data[0];
    })
}

const saveFinanRedeem=()=>{

    //赎回金额必须小于等于用户购买金额
    if(Number(UFRmoney.value)<=data.value.UF_Money){
        alert("赎回金额必须小于等于用户购买金额")
        return;
    }

    axios.post("https://localhost:44387/api/Finans/AddUserFinanRedeem",{
        UF_Id:route.query.UF_Id,
        UFR_Money:UFRmoney.value
    }).then(res=>{
        console.log(res.data)
        if(res.data){
            alert("用户理财赎回成功")
            router.push("/UserFinanRedeemInfo")
        }else{
            alert("用户理财赎回失败")
        }
    })
}
</script>
